package de.wellenblau.slideshow
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class MosaicSlideshow extends AbstractSlideshow
	{
		//config especially for this fade or tween
		private var _currentImage:Bitmap;
		private var _timer:Timer;
		private var _box:Sprite;
		private var _yMask:int=0;
		private var _xMask:int=0;
		private var _blockSize:int = 10;
		
		//tweenConfig
		private var _fadeOutTime:uint;
		private var _fadeOutDelay:Number;
		private var _fadeOutTransition:String;
		
		public function MosaicSlideshow()
		{
			super();
			
			_timer = new Timer(10);
			_timer.addEventListener(TimerEvent.TIMER,onTick);
			
			//the mask
			_box = new Sprite();
		}
		
		override protected function initConfig():void{
			//faderconfig
			_fadeOutTime = _config.fadeout.@time;
			_fadeOutDelay = _config.fadeout.@delay;
			_fadeOutTransition = _config.fadeout.@transition;
		}
		
		override protected function loadingComplete():void{
			//add the current image to the que
			if(_currentImage != null){
				_images.push(_currentImage);
			}
			//shift the next image out of the que
			_currentImage = _images.shift();
			
			//dispaly the current image
			addChild(_images[0]);
			addChild(_currentImage);
			addChild(_box);
			_images[0].alpha = 1;
			_currentImage.alpha = 1;	
			
			//start the mosaic timer
			_timer.start();
		}
		
		private function onTick(e:TimerEvent):void{
			
			//every tick the mask grow up
			_xMask += _blockSize;
			
			//complete x line
			if(_xMask >= _currentImage.width){
				_yMask += _blockSize; 	//one y line deeper
				_xMask = 0;				//next x line
			}
			
			//clear the mask
			_box.graphics.clear();
			//fill mask red
			_box.graphics.beginFill(0xff0000);
			//draw the current line
			_box.graphics.drawRect(
				_xMask,
				_yMask,
				_currentImage.width - _xMask,
				_blockSize
			);
			//draw the untouched lines
			_box.graphics.drawRect(
				0,
				_yMask + _blockSize,
				_currentImage.width,
				_currentImage.height - _yMask - _blockSize
			);
			_box.graphics.endFill();
			
			
			//mask it
			_currentImage.mask = _box;
			
			//ready
			if(_yMask > _currentImage.height - _blockSize){
				//delete the mask
				_currentImage.mask = null;
				//reset the line counter
				_xMask = _yMask = 0;
				//stop the timer
				_timer.stop();
				//next iteration
				loadingComplete();
			}
		}
	}
}